package com.example.demo.web;

import com.demo.sample.common.OperationResponse;
import com.example.demo.service.AccountService;
import io.seata.core.exception.TransactionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping("debit")
    public OperationResponse debit(Integer userId, BigDecimal money) throws TransactionException {
        Integer cnt = accountService.debit(userId, money);
        if (cnt == 0) {
            throw new RuntimeException("余额不足");
        }
        return OperationResponse.builder()
                .success(cnt>0)
                .message(cnt>0?"成功":"余额不足")
                .build();
    }
}
